package cs2340.todolist.model.sqlite_database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import cs2340.todolist.model.User;

public class UserAccountDataSource {
	
	// Database fields
	private SQLiteDatabase database;
	private UserAccountSQLiteHelper dbHelper;

	/**
	 * constructor for this class
	 * @param context the context to be used
	 */
	public UserAccountDataSource(Context context) {
		dbHelper = new UserAccountSQLiteHelper(context);
	}

	/**
	 * opens the databse
	 * @throws SQLException - exception thrown by database
	 */
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * closes the database
	 */
	public void close() {
		dbHelper.close();
	}
	
	/**
	 * changes the context of the database
	 * @param newContext - the new context to assign
	 */
	public void changeContext(Context newContext){
		dbHelper = new UserAccountSQLiteHelper(newContext);
	}
	
	
	/**
	 * retrieve the fields from the cursor to build a speakUpMessage
	 * @param cursor
	 * @return the constructed speakUpMessage
	 */
	private User cursorToUser(Cursor cursor) {
		User user = new User(
				cursor.getLong(cursor.getColumnIndex(UserAccountSQLiteHelper.COLUMN_ID)),
				cursor.getString(cursor.getColumnIndex(UserAccountSQLiteHelper.COLUMN_NAME)),
				cursor.getString(cursor.getColumnIndex(UserAccountSQLiteHelper.COLUMN_EMAIL)),
				cursor.getString(cursor.getColumnIndex(UserAccountSQLiteHelper.COLUMN_USERNAME)),
				cursor.getString(cursor.getColumnIndex(UserAccountSQLiteHelper.COLUMN_PASSWORD))
				);
			
		return user;
	}
	
	/**
	 * inserts a user to the table
	 * @param name - name of user
	 * @param email - email of user
	 * @param username - username of user
	 * @param password - password of user
	 * @return - user id
	 */
	public long insertUser(String name, String email, String username, String password) {
			System.out.println("trying to insert " + username);
			
			ContentValues toInsertValues = new ContentValues();
			toInsertValues.put(UserAccountSQLiteHelper.COLUMN_NAME, name);
			toInsertValues.put(UserAccountSQLiteHelper.COLUMN_EMAIL, email);
			toInsertValues.put(UserAccountSQLiteHelper.COLUMN_USERNAME, username);	
			toInsertValues.put(UserAccountSQLiteHelper.COLUMN_PASSWORD, password);	
			
			try {
				// To show how to insert, the insert method returns the id of the newly-inserted row
				return database.insert(UserAccountSQLiteHelper.TABLE_USERS, null, toInsertValues);
				
			}
			catch(SQLException ex) {
				ex.printStackTrace();
				return -1;
			}
			// To show how to query, the query method returns a cursor, 
			// note that if insertId = -1, there has been an error inserting the new row.
			/*
			if (insertId != -1) {
				Cursor cursor = database.query(
						ToDoListSQLiteHelper.TABLE_SPEAK_UP_MESSAGES,
						ToDoListSQLiteHelper.getAllColumnNames(),
						ToDoListSQLiteHelper.COLUMN_ID + " = " + insertId,
						null, null, null, null
						);
			
				cursor.moveToFirst();
				return cursorToSpeakUpMessage(cursor);
			}
			*/
			
	}
	
	/**
	 * Delete the specified user from the database
	 * @param user
	 */
	public void deleteUser(User user) {
		database.delete(
				UserAccountSQLiteHelper.TABLE_USERS,
				UserAccountSQLiteHelper.COLUMN_ID + " = " + user.getId(),
				null
				);
	}
	
	/**
	 * Delete the user with the given id from the database
	 * @param user
	 */
	public void deleteUser(long id) {
		database.delete(
				UserAccountSQLiteHelper.TABLE_USERS,
				UserAccountSQLiteHelper.COLUMN_ID + " = " + id,
				null
				);
	}
	
	
	/**
	 * @param type
	 * @return a list holding the users that has the specified type
	 */
	public User getUserbyUsername(String username) {
		
		Cursor cursor = database.query(
				UserAccountSQLiteHelper.TABLE_USERS,
				UserAccountSQLiteHelper.getAllColumnNames(),
				UserAccountSQLiteHelper.COLUMN_USERNAME + " = '" + username + "'",
				null, null, null, null
				);
		
		//catches if the username is not found
		if(cursor.isAfterLast()) return null;
		
		cursor.moveToFirst();
		User ret = cursorToUser(cursor);
		cursor.close();
		
		return ret;
	}
	
	
	/**
	 * @return a List containing all the toDoItems in the database
	 */
	/*
	public List<User> getAllUsers() {
		List<User> userList = new ArrayList<User>();
		
		//the following gets the cursor, a container that holds 
		//a number of columns and rows of the speak_up_messages table. 
		//In this case, we want the whole table
		Cursor cursor = database.query(
				UserAccountSQLiteHelper.TABLE_USERS, 
				UserAccountSQLiteHelper.getAllColumnNames(), 
				null, null, null, null, null
				);
		
		//go to the first row in the cursor
		cursor.moveToFirst();

		//loop the cursor until hitting the last row in the cursor and add each row to the result list
		while (!cursor.isAfterLast()) {
			userList.add(cursorToUser(cursor));
			cursor.moveToNext();
		}
		cursor.close();//close cursor to free memory space
		
		return userList;
	}
	
	*/
}